const express = require("express");
const bodyParser = require("body-parser");
const userModel = require("./models/useModel");
const underModel = require("./models/underModel");
let app = express();

app.use(bodyParser.json());

app.get("/",(req,res)=>{
 res.sendFile(__dirname+"/views/index.html");
})

app.get("/adddata",async (req,res)=>{
    let {username,message}=req.query
    let usedata = {
        username,message
    }
    console.log(usedata);
   await userModel.create(usedata);
   res.json({
    info:"添加成功",
    status:1,
   })
})
app.get("/refresh",async (req,res)=>{
   let data = await userModel.find()
   res.json({
    info:"添加成功",
    status:1,
    data
   })
})

app.get("/deldata",async (req,res)=>{
    let {_id}=req.query;
    await userModel.deleteOne({_id});
    res.json({
     info:"删除成功",
     status:1
    })
 })

app.get("/unddata",async (req,res)=>{
    let {val,parent_id}=req.query;
    // console.log(req.query)
    await underModel.create({val,parent_id:parent_id});
    res.json({
     info:"回复成功",
     status:1,
    })
 })

 app.get ("/newdata",async(req,res)=>{
    let data = await underModel.find();
    res.json({
        info:"查询成功",
        status:1,
        data
       })
 })



app.listen(9090);